﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XDataBase.Demo.Data.Model;
using XDataBase.Demo.Data.Script;
using XLugia.Lib.XDataBase.Base.Model;
using XLugia.Lib.XDataBase.Base.Lib;
using XLugia.Lib.XDataBase.Engine.Lib;

namespace XDataBase.Demo.Data.Controller
{
    public class TDemoController : BaseController
    {
        /// <summary>
        /// 获取分页数据
        /// </summary>
        public DatabaseResultModel<List<TDemoModel>> getListData(TDemoModel searchModel)
        {
            var result = new DatabaseResultModel<List<TDemoModel>>();
            try
            {
                using (databaseConnector = getDatabaseConnector())
                using (databaseExecutor = new Executor(databaseConnector))
                {
                    databaseScriptModel = new DatabaseScriptModel();
                    databaseScriptModel.command = (new TDemoScript()).getListData(searchModel);
                    result.data = databaseExecutor.sqlQueryToList<TDemoModel>(databaseScriptModel);
                }
            }
            catch (Exception ex) { result.exception = ex; }
            return result;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        public DatabaseResultModel<List<TDemoModel>> getData(TDemoModel model)
        {
            var result = new DatabaseResultModel<List<TDemoModel>>();
            try
            {
                using (databaseConnector = getDatabaseConnector())
                using (databaseExecutor = new Executor(databaseConnector))
                {
                    databaseScriptModel = new DatabaseScriptModel();
                    databaseScriptModel.command = (new TDemoScript()).getData(model);
                    databaseScriptModel.parameters = (new TDemoScript()).getDataParameters(model);
                    result.data = databaseExecutor.sqlQueryToList<TDemoModel>(databaseScriptModel);
                }
            }
            catch (Exception ex) { result.exception = ex; }
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        public DatabaseResultModel<bool> delete(TDemoModel model)
        {
            var result = new DatabaseResultModel<bool>();
            try
            {
                using (databaseConnector = getDatabaseConnector())
                using (databaseExecutor = new Executor(databaseConnector))
                {
                    databaseScriptModel = new DatabaseScriptModel();
                    databaseScriptModel.command = (new TDemoScript()).delete(model);
                    executeSqlCommand(new TDemoScript());
                    result.data = true;
                }
            }
            catch (Exception ex) { result.exception = ex; result.data = false; }
            return result;
        }

        /// <summary>
        /// 批量更新数据（新增和更新）
        /// </summary>
        public DatabaseResultModel<bool> updateMany(List<TDemoModel> models)
        {
            var result = new DatabaseResultModel<bool>();
            try
            {
                using (databaseConnector = getDatabaseConnector())
                using (databaseExecutor = new Executor(databaseConnector))
                {
                    databaseScriptModel = new DatabaseScriptModel();
                    databaseScriptModel.command = (new TDemoScript()).updateMany(models);
                    executeSqlCommand(new TDemoScript());
                    result.data = true;
                }
            }
            catch (Exception ex) { result.exception = ex; result.data = false; }
            return result;
        }
    }
}
